<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
    <style>
        #container {
            width: 520px;
            margin: 20px auto;
        }
        .input {
            margin: 20px 20px;
            width: 450px;
            height: 40px;
        }
        .input>label {
            display: inline-block;
            width: 150px;
            text-align: right;
        }
        a {
            text-decoration: none;
            color: darkcyan;
        }
        form+div {
            margin-top: 20px;
        }
        form+div>a {
            font-size: 1.2em;
        }
        .button {
            width: 520px;
            margin-top: 10px;
            text-align: center;
        }
        .button>div {
            text-align: left;
            margin: 10px 10px;
        }
        .hint {
            color: red;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div id="container">
        <h1>用户注册</h1>
        <hr>
        <p class="hint">{{ hint }}</p>
        <form action="" method="post">
            <fieldset>
                <legend>用户信息</legend>
                <div class="input">
                    <label>用户名：</label>
                    <input type="text" v-model="username">
                </div>
                <div class="input">
                    <label>密码：</label>
                    <input type="password" v-model="password">
                </div>
                <div class="input mobile">
                    <label>手机号：</label>
                    <input type="tel" v-model="tel">
                    <input type="button" value="发送验证码" @click="sendCode()" :disabled="isBlocked">
                </div>
                <div class="input">
                    <label>验证码：</label>
                    <input type="text" v-model="mobilecode">
                </div>
            </fieldset>
            <div class="button">
                <input type="submit" value="注册" @click.prevent="register()">
                <input type="reset" value="重置">
                <div>
                    <input type="checkbox" v-model="agreement">
                    我已经同意网站<a href="">用户协议及隐私政策</a>
                </div>
            </div>
        </form>
        <div>
            <a href="/">返回首页</a>
            <a href="/static/html/login.html">返回登录</a>
        </div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
    <script>
        let app = new Vue({
            el: '#container',
            data: {
                hint: '',
                username: '',
                password: '',
                tel: '',
                mobilecode: '',
                agreement: '',
                isBlocked: false
            },
            methods: {
                sendCode() {
                    fetch('/api/mobile/' + this.tel)
                        .then(resp => resp.json())
                        .then(json => {
                            alert(json.message)
                            if (json.code === 40000) {
                                this.isBlocked = true
                                setTimeout(() => {this.isBlocked = false}, 120000)
                            }
                        })
                },
                register() {
                    fetch('/register/', {
                        method: 'post',
                        body: JSON.stringify({
                            username: this.username,
                            password: this.password,
                            tel: this.tel,
                            mobilecode: this.mobilecode,
                            agreement: this.agreement
                        }),
                        headers: {'content-type': 'application/json'}
                    }).then(resp => resp.json()).then(json => {
                        if (json.code === 30000) {
                            location.href = '/static/html/login.html'
                        } else {
                            this.hint = json.mesg
                            this.username = ''
                            this.password = ''
                            this.tel = ''
                        }
                    })
                }
            }
        })
    </script>
</body>
</html>